package net.coding.li709.materialrefresher.listener;

import net.coding.li709.materialrefresher.keys.FlanData;
import net.coding.li709.materialrefresher.manage.BoxSpawnManager;
import net.coding.li709.materialrefresher.utils.ItemUtils;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.effect.potion.PotionEffect;
import org.spongepowered.api.effect.potion.PotionEffectTypes;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.EntityTypes;
import org.spongepowered.api.entity.Item;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.cause.entity.damage.DamageTypes;
import org.spongepowered.api.event.cause.entity.damage.source.DamageSource;
import org.spongepowered.api.event.entity.CollideEntityEvent;
import org.spongepowered.api.event.entity.DamageEntityEvent;
import org.spongepowered.api.event.entity.InteractEntityEvent;
import org.spongepowered.api.event.entity.SpawnEntityEvent;
import org.spongepowered.api.event.filter.Getter;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.filter.type.Include;
import org.spongepowered.api.event.game.state.GameStoppingServerEvent;
import org.spongepowered.api.item.inventory.entity.PlayerInventory;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


public class GameListener {


    /**
     * 实体碰撞实体（拾取物品处理）
     * 不拾取生成器生成的物品
     */
    @Listener
    public void fallEvent(CollideEntityEvent event, @First Player player) {
        event.filterEntities(entity -> {
            if (entity.getType().equals(EntityTypes.ITEM) || entity.getType().getId().equals("flansmod:customitem")) {
                Optional<FlanData> optionalFlanData = entity.get(FlanData.class);
                //如果实体里的isspawn为true，则不拾取
                return optionalFlanData.map(flanData -> !flanData.isSpawn()).orElse(true);
            }
            return true;
        });
    }

    /**
     * 右键生成的枪械，右键后会拾取
     */
    @Listener
    @Include({InteractEntityEvent.Primary.MainHand.class,InteractEntityEvent.Secondary.MainHand.class})
    public void fallEvent1(InteractEntityEvent event, @First Player player) {
        if (event.getTargetEntity().getType().getId().equals("flansmod:customitem")) {
            Item entityItem = (Item)event.getTargetEntity();
            //如果是生成点的道具，则通知生成点
            entityItem.get(FlanData.class).ifPresent((BoxSpawnManager::pickupSpawnLoc));
            //添加物品到玩家背包
            ItemUtils.addItem(entityItem.item().get().createStack(), (PlayerInventory) player.getInventory());
            entityItem.remove();
            event.setCancelled(true);
        }
    }

    /**
     * 丢出来的枪也要右键拾取
     */
    @Listener
    public void fallEvent1(SpawnEntityEvent event) {
        for (Entity entity : event.getEntities()) {
            //生成掉落物枪械时，如丢弃等操作，要写入右键拾取
            if (entity.getType().getId().equals("flansmod:customitem")){
                Optional<FlanData> flanData = entity.get(FlanData.class);
                if (!flanData.isPresent()){
                    entity.offer(new FlanData(true,""));
                }
            }
        }
    }

    /**
     * 生成的枪械物品，如果是生成点的，需要把生成点的状态修改为未拾取
     */
    @Listener
    public void spawnCustomitemEvent(SpawnEntityEvent event) {
        for (Entity entity : event.getEntities()) {
            if (entity.getType().getId().equals("flansmod:customitem")){
                entity.get(FlanData.class).ifPresent(BoxSpawnManager::changeSpawnLocState);
            }
        }
    }

    /**
     * 游戏关闭。关闭定时调度
     * @param event
     */
    @Listener
    public void gameStoppingServerEvent(GameStoppingServerEvent event){
        BoxSpawnManager.stop();
    }

}
